﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using EnvDTE;
using VSLangProj;
using System.Reflection;

namespace Arthanarieaswaran.CodeC_Package
{
    /// <summary>
    /// Interaction logic for MyControl.xaml
    /// </summary>
    public partial class MyControl : UserControl
    {
        public MyControl()
        {
            InitializeComponent();
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1300:SpecifyMessageBoxOptions")]
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ReferencesLoad(LoadProjectItem(ServiceProvider));

        }

        public System.IServiceProvider ServiceProvider;
        private Project LoadProjectItem(System.IServiceProvider serviceProvider)
        {

            if (null != serviceProvider)
            {
                EnvDTE80.DTE2 dte = (EnvDTE80.DTE2)serviceProvider.GetService(typeof(Microsoft.VisualStudio.Shell.Interop.SDTE));
                if (null != dte && null != dte.Solution)
                    foreach (Project item in dte.Solution.Projects)
                        return item;

            }


            return null;

        }

        private void ReferencesLoad(Project project)
        {
            VSLangProj.VSProject vsProject = (VSLangProj.VSProject)project.Object;

            References refs = vsProject.References;
            int length = refs.Count;
            List<string> listOfReferences = new List<string>();
            List<string> listOfNamespaces = new List<string>();
            for (int i = 1; i <= length; i++)
            {
                listOfReferences.Add(refs.Item(i).Path);
            }


            foreach (var item in listOfReferences)
            {
                try
                {


                    var assembly = Assembly.LoadFile(item);
                    foreach (var assemblyType in assembly.GetTypes())
                    {
                        if (!listOfNamespaces.Contains(assemblyType.Namespace))
                        {
                            listOfNamespaces.Add(assemblyType.Namespace);

                        }
                    }
                }
                catch (Exception)
                {

                   // throw;
                }
             

            }

            //var reff = vsProject.References;
            //string referenceDirectory = Path.GetDirectoryName(vsProject.References.Find("System").Path);

            //if (null != listOfReferences)
            //    foreach (var item in listOfReferences)
            //        if (File.Exists(referenceDirectory + Path.DirectorySeparatorChar + item + ".dll"))
            //            vsProject.References.Add(referenceDirectory + Path.DirectorySeparatorChar + item + ".dll");


        }

    }
}